package com.qtummatrix.dao.impl;

import com.qtummatrix.dao.BlacklistDao;
import com.qtummatrix.entity.BlackList;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * create by Gongshiyong  2019-11-21 19:39
 */

@Repository
public class BlacklistDaoImpl implements BlacklistDao {

    @Autowired
    @Qualifier("sessionFactory")
    public SessionFactory sessionFactory;


    public Session getSession() {
        return sessionFactory.getCurrentSession();
    }


    public List<BlackList> findBlacklist() {
        String sql = "from BlackList where state='已审核'";
        List<BlackList> list = (List) getSession().createQuery(sql).list();

        return list;
    }

    public List<BlackList> findBlacklist(String UID, String name, String source, String state, int first, int rows) {
        StringBuilder sql = new StringBuilder("from BlackList where state='已审核' ");
        System.out.println(name);
        boolean bUID = UID == null || UID.equals("");
        boolean bname = name == null || name.equals("");
        boolean bsource = source == null || source.equals("");
        boolean bstate = state == null || state.equals("");


        if (!bname) {
            sql.append(" and name=:name");
        }
        if (!bUID) {
            sql.append("  and UID=:UID");
        }

        if (!bsource) {
            sql.append("  and source=:source");
        }
        Query query = getSession().createQuery(sql.toString());
        if (!bname) {
            query.setString("name", name);
        }
        if (!bUID) {
            query.setString("UID", UID);
        }

        if (!bstate) {
            query.setString("source", source);
        }
        List<BlackList> list = query.setFirstResult(first).setMaxResults(rows).list();

        return list;
    }

    public int addBlacklist(String UID, String name, String mark, String type, String source) {
		/*String sql="insert into BlackList(UID,name,mark,type) values(?,?,?,?)";
		int count =getSession().createQuery(sql).setString(0, UID).setString(1,name)
				.setString(2,mark).setString(3, type).executeUpdate();*/
        //获取当前时间
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String submittime = dateFormat.format(date);

        BlackList blackList = new BlackList();
        blackList.setName(name);
        blackList.setMark(mark);
        blackList.setType(type);
        blackList.setUID(UID);
        blackList.setSource(source);

        blackList.setSubmittime(submittime);
        blackList.setState("未审核");
        int count = (Integer) getSession().save(blackList);
        return count;
    }


    public List<BlackList> findBlacklistAudit(String UID, String name, String source, String type, int first,
                                              int rows) {
        StringBuilder sql = new StringBuilder("from BlackList where state='未审核'   ");
        boolean bUID = UID == null || UID.equals("");
        boolean bname = name == null || name.equals("");
        boolean bsource = source == null || source.equals("");
        boolean btype = type == null || type.equals("");
        if (!bUID) {
            sql.append(" and UID=:UID ");
        }
        if (!bname) {
            sql.append(" and name=:name ");

        }
        if (!btype) {
            sql.append(" and type=:type ");
        }
        Query query = getSession().createQuery(sql.toString());

        if (!bUID) {
            query.setString("UID", UID);
        }
        if (!bname) {
            query.setString("name", name);

        }
        if (!btype) {
            query.setString("type", type);
        }

        List<BlackList> list = query.setFirstResult(first).setMaxResults(rows).list();

        System.out.println(list);
        return list;
    }


    public List<BlackList> findBlacklistAudit() {
        String sql = "from BlackList where state='未审核'";
        List<BlackList> list = (List) getSession().createQuery(sql).list();

        return list;
    }


    public void deleteBlacklistAudit(int id) {
        BlackList blackList = new BlackList();
        blackList.setId(id);
        getSession().delete(blackList);

    }


    public void updateBlackAudit(int id) {
        String sql = "update BlackList set state='已审核' where id=?";
        getSession().createQuery(sql).setInteger(0, id).executeUpdate();


    }

}
